package com.zzld.modules.autoinn.action;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Customer;
import com.zzld.modules.autoinn.entity.Recharge;
import com.zzld.modules.autoinn.service.ICustomerService;
import com.zzld.modules.autoinn.service.IRechargeService;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.sys.entity.User;
import com.zzld.modules.sys.security.ShiroUser;

@Controller
@RequestMapping("/autoinn/recharge")
public class RechargeController {

	@Autowired
	private IRechargeService rechargeService;

	@Autowired
	private ICustomerService customerService;

	private String listurl = "/modules/autoinn/recharge/rechargeList";
	private String listcru = "/modules/autoinn/recharge/rechargeCru";

	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return listurl;
	}

	@RequestMapping(value = "inser")
	public String cru(HttpServletRequest request) {
		return listcru;
	}

	@RequestMapping(value = "list")
	@ResponseBody
	public CommonJSON lista(Model model, HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("nickname", request.getParameter("nickname"));
		map.put("startime", request.getParameter("starinnertime"));
		map.put("endtime", request.getParameter("endinnertime"));
		map.put("sort", request.getParameter("sort"));
		map.put("order", request.getParameter("order"));

		Integer pageNum = Integer.parseInt(request.getParameter("page"));
		Integer pageSize = Integer.parseInt(request.getParameter("rows"));

		PageInfo page = rechargeService.list(map, pageNum, pageSize);
		json.setRows(page.getList());
		json.setTotal(page.getTotal());
		return json;
	}

	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON del(Model model, HttpServletRequest request,
			@PathVariable("id") String id) {
		CommonJSON json = new CommonJSON();
		try {
			if (rechargeService.deleteByPrimaryKey(id) > 0) {
				json.setMessage(MessageUtils.DELETE_SUCCESS);
				json.setSuccess(true);
			} else {
				json.setMessage(MessageUtils.DELETE_WARNING);
				json.setSuccess(false);
			}
		} catch (Exception e) {
			json.setMessage(MessageUtils.DELETE_WARNING);
			json.setSuccess(false);
		}

		return json;
	}

	@RequestMapping(value = "exportXls")
	public String exportXls(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		Map map = new HashMap<String, Object>();
		map.put("nickname", null);
		map.put("startime", null);
		map.put("endtime", null);
		map.put("sort", "innertime");
		map.put("order", "desc");

		ShiroUser userq = (ShiroUser) SecurityUtils.getSubject().getPrincipal();

		List<Recharge> list = rechargeService.selectall(map);
		modelMap.put(NormalExcelConstants.FILE_NAME, "充值信息表");
		modelMap.put(NormalExcelConstants.CLASS, Recharge.class);
		modelMap.put(NormalExcelConstants.PARAMS, new ExportParams("充值信息表",
				"导出人:" + userq.getName(), "导出信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST, list);
		return NormalExcelConstants.JEECG_EXCEL_VIEW;

	}

	@RequestMapping("/add")
	@ResponseBody
	public CommonJSON add(HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		try {
			Recharge r = new Recharge();
			r.setId(UuidUtil.uuid().toString());
			r.setCustomer_id(request.getParameter("customerid"));
			Example e = new Example(Customer.class);
			e.createCriteria().andEqualTo("customer_id",
					request.getParameter("customerid"));

			String wxaccount = customerService.selectByExample(e).get(0)
					.getWxAccount();
			r.setWx_account(wxaccount);
			r.setInnertime(new Date());
			r.setPrice(BigDecimal.valueOf(Double.valueOf(request
					.getParameter("price"))));
			r.setDes(request.getParameter("des"));
			if (rechargeService.saveSelective(r) > 0) {
			    Customer c1 = new Customer();
			    
			    c1.setSurplus(BigDecimal.valueOf(Double.valueOf(customerService.selectByExample(e).get(0).getSurplus().toString())+Double.valueOf(r.getPrice().toString())));
			    c1.setCustomerId(r.getCustomer_id());
				customerService.updateByPrimaryKeySelective(c1);
				json.setMessage(MessageUtils.ADD_SUCCESS);
				json.setSuccess(true);
			}
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			json.setMessage(MessageUtils.ADD_FAIL);
			json.setSuccess(false);
		}

		return json;
	}

}
